<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>帖子</title>
    <link rel="stylesheet" href="/static/index1.css">

</head>
<body>
<h1>帖子</h1>


<table id="postTable">
    <thead>
    <tr>
        <th>图片</th>
        <th>标题</th>
        <th>简介</th>
        <th>查看详情</th>
    </tr>
    </thead>
    <tbody>
    <!-- 商品列表将在这里动态生成 -->
    </tbody>
</table>
<script>
    function getQueryParam(paramName) {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get(paramName);
    }
    const ip = getQueryParam('id');//获取url参数
    function fetchpost(){
        url=`/type/post?id=${ip}`;
        console.log(url)
        fetch(url) // 注意添加 http:// 或 https://
            .then(response => {
                console.log(response.status); // 打印状态码
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(data => {
                const tbody = document.querySelector('#postTable tbody');
                tbody.innerHTML = ''; // 清空之前的商品列表
                console.log(data.data)
                data.data.forEach(post => {
                    const row = document.createElement('tr');

// 图片
                    const photoCell = document.createElement('td');
                    const imgElement = document.createElement('img');
                    imgElement.src = post.imageurl;
                    imgElement.style.maxWidth = '200px'; // 例如，设置最大宽度
                    imgElement.style.maxHeight = '120px'; // 例如，设置最大高度
                    photoCell.appendChild(imgElement);
                    row.appendChild(photoCell);

// 标题
                    const titleCell = document.createElement('td');
                    titleCell.textContent = post.title;
                    row.appendChild(titleCell);

// 简介
                    const briefCell = document.createElement('td');
                    briefCell.textContent = post.brief;
                    row.appendChild(briefCell);


//创建新的一列
                    const actionButtonCell = document.createElement('td');
// 查看卖家信息按钮
                    const moreButton = document.createElement('button');
                    moreButton.textContent = '展开';
                    moreButton.onclick = function() {
                        window.location.href = `/api/v1/post/searchpidHTML?id=${post.pid}`; // 假设详情页HTML
                    };
                    actionButtonCell.appendChild(moreButton);
                    actionButtonCell.appendChild(document.createTextNode(' ')); // 添加间隔


                    row.appendChild(actionButtonCell);

// 将行添加到表格中
                    tbody.appendChild(row);
                });
            })
            .catch(error => {
                console.error('Error fetching post:', error);
                document.getElementById('postList').textContent = 'Error fetching post information.';
            });

    }
    window.onload = fetchpost;
</script>
</body>
</html>
